Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Properly format switch expressions #1069

Merged
merged 2 commits into from
Apr 5, 2024
Merged

Conversation

johnhany97
Copy link
Member

@johnhany97 johnhany97 commented Apr 5, 2024

Before this PR

Fixes #1025 where before this change, we'd format switch expressions used in an assignment to yield the following:

    public void test2(int y) {
        int x;
        x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();};
    }

After this PR

==COMMIT_MSG==
Properly format switch expressions
==COMMIT_MSG==
and now we generate this:

    public void test2(int y) {
        int x;
        x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();
        };
    }

Possible downsides?

@@ -63,6 +63,7 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines() throws IOExc
runCommandInRepo("git", "init");
runCommandInRepo("git", "config", "user.name", "Test User");
runCommandInRepo("git", "config", "user.email", "[email protected]");
runCommandInRepo("git", "config", "commit.gpgsign", "false");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by improvement: Didn't see it as necessary to sign the commits made by the test

Comment on lines +52 to +57
int x =
switch (y) {
case 1 -> 1;
case 2 -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();
};
Copy link
Member Author

@johnhany97 johnhany97 Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to fix this in the future FWIW to instead be:

        int x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();
        };

But that requires more complicated changes to happen to the visitAssignment method which should be left to a separate PR

Copy link
Contributor

@carterkozak carterkozak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tyvm!

@bulldozer-bot bulldozer-bot bot merged commit 1f52b40 into develop Apr 5, 2024
7 checks passed
@bulldozer-bot bulldozer-bot bot deleted the jayad/fix-switch-expr branch April 5, 2024 17:34
@svc-autorelease
Copy link
Collaborator

Released 2.42.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Poor formatting around expression switches
4 participants